#include <common.h>
#include <windows.h>
#include "glib.h"
#include "Engine.h"
#include "config.h"


CGraphics::CGraphics(CEngine *engine)
:m_color(0),
m_engine(engine)
{
	m_mathTool.GetSinCos();

	m_camera.m_camPosX = 0;
	m_camera.m_camPosY = 0;
	m_camera.m_camPosZ = 0;
}

CGraphics::~CGraphics()
{
	m_engine = NULL;
}

void CGraphics::gSetColor(unsigned char r, unsigned char g, unsigned char b)
{
	m_color = RGB32(r, g, b);
}

void CGraphics::gInsertToRenderList(CTriangleIndex *tri)
{
	gVertex3D *a, *b, *c;

	a = &tri->m_vertexBuffer[tri->indexA];
	b = &tri->m_vertexBuffer[tri->indexB];
	c = &tri->m_vertexBuffer[tri->indexC];

	m_renderList.AddFace(a, b, c);
}

int CGraphics::gClipLine(int &x1,int &y1,int &x2, int &y2)
{
	// this function clips the sent line using the globally defined clipping
	// region

	// internal clipping codes
#define CLIP_CODE_C  0x0000
#define CLIP_CODE_N  0x0008
#define CLIP_CODE_S  0x0004
#define CLIP_CODE_E  0x0002
#define CLIP_CODE_W  0x0001

#define CLIP_CODE_NE 0x000a
#define CLIP_CODE_SE 0x0006
#define CLIP_CODE_NW 0x0009 
#define CLIP_CODE_SW 0x0005

	int xc1=x1, 
		yc1=y1, 
		xc2=x2, 
		yc2=y2;

	int min_clip_x = 0;
	int max_clip_x = 0;
	int min_clip_y = 0;
	int max_clip_y = 0;
	int cw = 0;
	int ch = 0;

	m_engine->GetDisplay()->GetClip(&min_clip_x, &min_clip_y, &cw, &ch);
	max_clip_x = min_clip_x + cw;
	max_clip_y = min_clip_y + ch;

	int p1_code=0, 
		p2_code=0;

	// determine codes for p1 and p2
	if (y1 < min_clip_y)
		p1_code|=CLIP_CODE_N;
	else
		if (y1 > max_clip_y)
			p1_code|=CLIP_CODE_S;

	if (x1 < min_clip_x)
		p1_code|=CLIP_CODE_W;
	else
		if (x1 > max_clip_x)
			p1_code|=CLIP_CODE_E;

	if (y2 < min_clip_y)
		p2_code|=CLIP_CODE_N;
	else
		if (y2 > max_clip_y)
			p2_code|=CLIP_CODE_S;

	if (x2 < min_clip_x)
		p2_code|=CLIP_CODE_W;
	else
		if (x2 > max_clip_x)
			p2_code|=CLIP_CODE_E;

	// try and trivially reject
	if ((p1_code & p2_code)) 
		return(0);

	// test for totally visible, if so leave points untouched
	if (p1_code==0 && p2_code==0)
		return(1);

	// determine end clip point for p1
	switch(p1_code)
	{
	case CLIP_CODE_C: break;

	case CLIP_CODE_N:
		{
			yc1 = min_clip_y;
			xc1 = x1 + 0.5+(min_clip_y-y1)*(x2-x1)/(y2-y1);
		} break;
	case CLIP_CODE_S:
		{
			yc1 = max_clip_y;
			xc1 = x1 + 0.5+(max_clip_y-y1)*(x2-x1)/(y2-y1);
		} break;

	case CLIP_CODE_W:
		{
			xc1 = min_clip_x;
			yc1 = y1 + 0.5+(min_clip_x-x1)*(y2-y1)/(x2-x1);
		} break;

	case CLIP_CODE_E:
		{
			xc1 = max_clip_x;
			yc1 = y1 + 0.5+(max_clip_x-x1)*(y2-y1)/(x2-x1);
		} break;

		// these cases are more complex, must compute 2 intersections
	case CLIP_CODE_NE:
		{
			// north hline intersection
			yc1 = min_clip_y;
			xc1 = x1 + 0.5+(min_clip_y-y1)*(x2-x1)/(y2-y1);

			// test if intersection is valid, of so then done, else compute next
			if (xc1 < min_clip_x || xc1 > max_clip_x)
			{
				// east vline intersection
				xc1 = max_clip_x;
				yc1 = y1 + 0.5+(max_clip_x-x1)*(y2-y1)/(x2-x1);
			} // end if

		} break;

	case CLIP_CODE_SE:
		{
			// south hline intersection
			yc1 = max_clip_y;
			xc1 = x1 + 0.5+(max_clip_y-y1)*(x2-x1)/(y2-y1);	

			// test if intersection is valid, of so then done, else compute next
			if (xc1 < min_clip_x || xc1 > max_clip_x)
			{
				// east vline intersection
				xc1 = max_clip_x;
				yc1 = y1 + 0.5+(max_clip_x-x1)*(y2-y1)/(x2-x1);
			} // end if

		} break;

	case CLIP_CODE_NW: 
		{
			// north hline intersection
			yc1 = min_clip_y;
			xc1 = x1 + 0.5+(min_clip_y-y1)*(x2-x1)/(y2-y1);

			// test if intersection is valid, of so then done, else compute next
			if (xc1 < min_clip_x || xc1 > max_clip_x)
			{
				xc1 = min_clip_x;
				yc1 = y1 + 0.5+(min_clip_x-x1)*(y2-y1)/(x2-x1);	
			} // end if

		} break;

	case CLIP_CODE_SW:
		{
			// south hline intersection
			yc1 = max_clip_y;
			xc1 = x1 + 0.5+(max_clip_y-y1)*(x2-x1)/(y2-y1);	

			// test if intersection is valid, of so then done, else compute next
			if (xc1 < min_clip_x || xc1 > max_clip_x)
			{
				xc1 = min_clip_x;
				yc1 = y1 + 0.5+(min_clip_x-x1)*(y2-y1)/(x2-x1);	
			} // end if

		} break;

	default:break;

	} // end switch

	// determine clip point for p2
	switch(p2_code)
	{
	case CLIP_CODE_C: break;

	case CLIP_CODE_N:
		{
			yc2 = min_clip_y;
			xc2 = x2 + (min_clip_y-y2)*(x1-x2)/(y1-y2);
		} break;

	case CLIP_CODE_S:
		{
			yc2 = max_clip_y;
			xc2 = x2 + (max_clip_y-y2)*(x1-x2)/(y1-y2);
		} break;

	case CLIP_CODE_W:
		{
			xc2 = min_clip_x;
			yc2 = y2 + (min_clip_x-x2)*(y1-y2)/(x1-x2);
		} break;

	case CLIP_CODE_E:
		{
			xc2 = max_clip_x;
			yc2 = y2 + (max_clip_x-x2)*(y1-y2)/(x1-x2);
		} break;

		// these cases are more complex, must compute 2 intersections
	case CLIP_CODE_NE:
		{
			// north hline intersection
			yc2 = min_clip_y;
			xc2 = x2 + 0.5+(min_clip_y-y2)*(x1-x2)/(y1-y2);

			// test if intersection is valid, of so then done, else compute next
			if (xc2 < min_clip_x || xc2 > max_clip_x)
			{
				// east vline intersection
				xc2 = max_clip_x;
				yc2 = y2 + 0.5+(max_clip_x-x2)*(y1-y2)/(x1-x2);
			} // end if

		} break;

	case CLIP_CODE_SE:
		{
			// south hline intersection
			yc2 = max_clip_y;
			xc2 = x2 + 0.5+(max_clip_y-y2)*(x1-x2)/(y1-y2);	

			// test if intersection is valid, of so then done, else compute next
			if (xc2 < min_clip_x || xc2 > max_clip_x)
			{
				// east vline intersection
				xc2 = max_clip_x;
				yc2 = y2 + 0.5+(max_clip_x-x2)*(y1-y2)/(x1-x2);
			} // end if

		} break;

	case CLIP_CODE_NW: 
		{
			// north hline intersection
			yc2 = min_clip_y;
			xc2 = x2 + 0.5+(min_clip_y-y2)*(x1-x2)/(y1-y2);

			// test if intersection is valid, of so then done, else compute next
			if (xc2 < min_clip_x || xc2 > max_clip_x)
			{
				xc2 = min_clip_x;
				yc2 = y2 + 0.5+(min_clip_x-x2)*(y1-y2)/(x1-x2);	
			} // end if

		} break;

	case CLIP_CODE_SW:
		{
			// south hline intersection
			yc2 = max_clip_y;
			xc2 = x2 + 0.5+(max_clip_y-y2)*(x1-x2)/(y1-y2);	

			// test if intersection is valid, of so then done, else compute next
			if (xc2 < min_clip_x || xc2 > max_clip_x)
			{
				xc2 = min_clip_x;
				yc2 = y2 + 0.5+(min_clip_x-x2)*(y1-y2)/(x1-x2);	
			} // end if

		} break;

	default:break;

	} // end switch

	// do bounds check
	if ((xc1 < min_clip_x) || (xc1 > max_clip_x) ||
		(yc1 < min_clip_y) || (yc1 > max_clip_y) ||
		(xc2 < min_clip_x) || (xc2 > max_clip_x) ||
		(yc2 < min_clip_y) || (yc2 > max_clip_y) )
	{
		return(0);
	} // end if

	// store vars back
	x1 = xc1;
	y1 = yc1;
	x2 = xc2;
	y2 = yc2;

	return(1);
}

int CGraphics::gDrawLine32(int x_start, int y_start, int x_end, int y_end, bool useClip)
{
	unsigned char *buffer = m_engine->GetDisplay()->GetVideoBuffer();
	int width = (m_engine->GetDisplay()->GetPitch())/4;

	if(useClip)
	{
		int clipResult = gClipLine(x_start, y_start, x_end, y_end);
		if(clipResult == 0)
			return 0;
	}

	unsigned int *start = (unsigned int *)buffer + x_start + y_start * width;
	unsigned int color = m_color;
	int dx, dy, dx2, dy2, x_inc, y_inc, err, index;
    dx = x_end - x_start;
	dy = y_end - y_start;

	if (dx >= 0)
	{
		x_inc = 1;
	}
	else
	{
		x_inc = -1;
		dx = -dx;
	}
	if (dy >= 0)
	{
		y_inc = width;
	} 
	else
	{
		y_inc = -width;
		dy = -dy;
	}
	dx2 = dx << 1;
	dy2 = dy << 1;
	if (dx > dy)
	{
		err = dy2 - dx;
		for (index=0; index<=dx; index++)
		{
			*start = color;
			if (err >= 0)
			{
				err -= dx2;
				start += y_inc;
			}
			err += dy2;
			start += x_inc;
		}
	} 
	else
	{
		err = dx2 - dy;
		for (index=0; index<=dy; index++)
		{
			*start = color;
			if (err >= 0)
			{
				err -= dy2;
				start += x_inc;
			}
			err += dx2;
            start += y_inc;
		}
	}
	return 1;
}

int CGraphics::gFillRect32(int x_start, int y_start, int width, int height)
{
	unsigned char *buffer = m_engine->GetDisplay()->GetVideoBuffer();
	int mempitch = (m_engine->GetDisplay()->GetPitch())/4;

	int x,y;
	unsigned int *start = (unsigned int *)buffer + x_start + y_start * mempitch;
	unsigned int color = m_color;
	
	for (y=0;y<height;y++)
	{
		for (x=0;x<width;x++)
		{
			start[x+y*mempitch]=color;
		}
	}
	return 1;
}

void CGraphics::gDrawClipRect()
{
	gSetColor(255, 0, 0);
	int cx, cy, cw, ch;
	m_engine->GetDisplay()->GetClip(&cx, &cy, &cw, &ch);
	gDrawLine32(cx, cy, cx + cw - 1, cy);
	gDrawLine32(cx + cw - 1, cy, cx + cw - 1, cy + ch - 1);
	gDrawLine32(cx + cw - 1, cy + ch - 1, cx, cy + ch - 1);
	gDrawLine32(cx, cy + ch - 1, cx, cy);
}

void CGraphics::gDrawTriangle32(int x1, int y1, int x2, int y2, int x3, int y3)
{
	gDrawLine32(x1, y1, x2, y2);
	gDrawLine32(x2, y2, x3, y3);
	gDrawLine32(x3, y3, x1, y1);
}

void  CGraphics::gDrawTriangle32(CTriangleVertex *tri)
{
	int x1, y1, x2, y2, x3, y3;

	x1 = tri->va.m_pos.x;
	y1 = tri->va.m_pos.y;
	x2 = tri->vb.m_pos.x;
	y2 = tri->vb.m_pos.y;
	x3 = tri->vc.m_pos.x;
	y3 = tri->vc.m_pos.y;

	gDrawTriangle32(x1, y1, x2, y2, x3, y3);
}

void CGraphics::gDrawRenderList()
{
	for(int i=0; i<m_renderList.m_faceNum; ++i)
	{
		if(m_renderList.list[i].m_state == CTriangle::STATE_VISIABLE)
		{
			gDrawTriangle32(&m_renderList.list[i]);
		}
	}
}

void CGraphics::gResetRenderList()
{
	m_renderList.ResetList();
}

void CGraphics::gLoadIdentity()
{
	m_matModel.LoadIdentity();
}

void CGraphics::gPushMatrix()
{

}

void CGraphics::gPopMatrix()
{

}

void CGraphics::gSetViewPosX(int x)
{
	m_camera.m_camPosX = x;
}

void CGraphics::gSetViewPosY(int y)
{
	m_camera.m_camPosY = y;
}

void CGraphics::gSetViewPosZ(int z)
{
	m_camera.m_camPosZ = z;
}

void CGraphics::gSetViewWidth(int w)
{
}

void CGraphics::gSetViewHeight(int h)
{
}

int CGraphics::gGetViewPosX()
{
	return m_camera.m_camPosX;
}

int CGraphics::gGetViewPosY()
{
	return m_camera.m_camPosY;
}

int CGraphics::gGetViewPosZ()
{
	return m_camera.m_camPosZ;
}

int CGraphics::gGetViewWidth()
{
	return 0;
}

int CGraphics::gGetViewHeight()
{
	return 0;
}

void CGraphics::gTranslate(int tx, int ty, int tz)
{
	CGLIBMatrix tmp;
	tmp.LoadIdentity();
	tmp.SetElement((tx*1.0), 3, 0);
	tmp.SetElement((ty*1.0), 3, 1);
	tmp.SetElement((tz*1.0), 3, 2);
	m_matModel.MultMatrix(tmp);
}

void CGraphics::gRotateX(int rx)
{
	CGLIBMatrix tmp;
	tmp.LoadIdentity();
	tmp.SetElement(m_mathTool.myCos[rx], 1, 1);
	tmp.SetElement(m_mathTool.mySin[rx], 1, 2);
	tmp.SetElement(-m_mathTool.mySin[rx], 2, 1);
	tmp.SetElement(m_mathTool.myCos[rx], 2, 2);
	m_matModel.MultMatrix(tmp);
}

void CGraphics::gRotateY(int ry)
{
	CGLIBMatrix tmp;
	tmp.LoadIdentity();
	tmp.SetElement(m_mathTool.myCos[ry], 0, 0);
	tmp.SetElement(-m_mathTool.mySin[ry], 0, 2);
	tmp.SetElement(m_mathTool.mySin[ry], 2, 0);
	tmp.SetElement(m_mathTool.myCos[ry], 2, 2);
	m_matModel.MultMatrix(tmp);
}

void CGraphics::gRotateZ(int rz)
{
	CGLIBMatrix tmp;
	tmp.LoadIdentity();
	tmp.SetElement(m_mathTool.myCos[rz], 0, 0);
	tmp.SetElement(m_mathTool.mySin[rz], 0, 1);
	tmp.SetElement(-m_mathTool.mySin[rz], 1, 0);
	tmp.SetElement(m_mathTool.myCos[rz], 1, 1);
	m_matModel.MultMatrix(tmp);
}

void CGraphics::gScale(int sx, int sy, int sz)
{
	CGLIBMatrix tmp;
	tmp.LoadIdentity();
	tmp.SetElement((sx*1.0), 0, 0);
	tmp.SetElement((sy*1.0), 1, 1);
	tmp.SetElement((sz*1.0), 2, 2);
	m_matModel.MultMatrix(tmp);
}

void CGraphics::gTranslateToWorldCoord(gVertex3D *des, gVertex3D *src, int vertexNum)
{
	for(int i=0; i<vertexNum; i++)
	{
		des[i].m_pos.x = 
			src[i].m_pos.x*m_matModel.m[0][0] +
			src[i].m_pos.y*m_matModel.m[1][0] +
			src[i].m_pos.z*m_matModel.m[2][0] +
			src[i].m_pos.w*m_matModel.m[3][0];
		des[i].m_pos.y = 
			src[i].m_pos.x*m_matModel.m[0][1] +
			src[i].m_pos.y*m_matModel.m[1][1] +
			src[i].m_pos.z*m_matModel.m[2][1] +
			src[i].m_pos.w*m_matModel.m[3][1];
		des[i].m_pos.z = 
			src[i].m_pos.x*m_matModel.m[0][2] +
			src[i].m_pos.y*m_matModel.m[1][2] +
			src[i].m_pos.z*m_matModel.m[2][2] +
			src[i].m_pos.w*m_matModel.m[3][2];
		des[i].m_pos.w = 
			src[i].m_pos.x*m_matModel.m[0][3] +
			src[i].m_pos.y*m_matModel.m[1][3] +
			src[i].m_pos.z*m_matModel.m[2][3] +
			src[i].m_pos.w*m_matModel.m[3][3];
	}
}

void CGraphics::gTranslateToCameraCoord(gVertex3D *vbuffer, gVertex3D::VertexState *vertexStateBuff, int vertexNum)
{
	m_matModel.LoadIdentity();
	m_matModel.MultMatrix(m_camera.toCam);

	gVertex3D tmp;

	for(int i=0; i<vertexNum; i++)
	{
		if(vertexStateBuff[i] == gVertex3D::STATE_VISIABLE)
		{
			tmp.m_pos.x = 
				vbuffer[i].m_pos.x*m_matModel.m[0][0] +
				vbuffer[i].m_pos.y*m_matModel.m[1][0] +
				vbuffer[i].m_pos.z*m_matModel.m[2][0] +
				vbuffer[i].m_pos.w*m_matModel.m[3][0];
			tmp.m_pos.y = 
				vbuffer[i].m_pos.x*m_matModel.m[0][1] +
				vbuffer[i].m_pos.y*m_matModel.m[1][1] +
				vbuffer[i].m_pos.z*m_matModel.m[2][1] +
				vbuffer[i].m_pos.w*m_matModel.m[3][1];
			tmp.m_pos.z = 
				vbuffer[i].m_pos.x*m_matModel.m[0][2] +
				vbuffer[i].m_pos.y*m_matModel.m[1][2] +
				vbuffer[i].m_pos.z*m_matModel.m[2][2] +
				vbuffer[i].m_pos.w*m_matModel.m[3][2];
			tmp.m_pos.w = 
				vbuffer[i].m_pos.x*m_matModel.m[0][3] +
				vbuffer[i].m_pos.y*m_matModel.m[1][3] +
				vbuffer[i].m_pos.z*m_matModel.m[2][3] +
				vbuffer[i].m_pos.w*m_matModel.m[3][3];

			vbuffer[i].m_pos.x = tmp.m_pos.x;
			vbuffer[i].m_pos.y = tmp.m_pos.y;
			vbuffer[i].m_pos.z = tmp.m_pos.z;
			vbuffer[i].m_pos.w = tmp.m_pos.w;
		}
	}
}

void CGraphics::gApplyProject()
{
	gVertex3D tmp;

	for(int i=0; i<m_renderList.m_faceNum; ++i)
	{
		if(m_renderList.list[i].m_state != CTriangle::STATE_VISIABLE)
			continue;

		tmp.m_pos.x = 
			m_renderList.list[i].va.m_pos.x*m_camera.toPer.m[0][0] +
			m_renderList.list[i].va.m_pos.y*m_camera.toPer.m[1][0] +
			m_renderList.list[i].va.m_pos.z*m_camera.toPer.m[2][0] +
			m_renderList.list[i].va.m_pos.w*m_camera.toPer.m[3][0];
		tmp.m_pos.y = 
			m_renderList.list[i].va.m_pos.x*m_camera.toPer.m[0][1] +
			m_renderList.list[i].va.m_pos.y*m_camera.toPer.m[1][1] +
			m_renderList.list[i].va.m_pos.z*m_camera.toPer.m[2][1] +
			m_renderList.list[i].va.m_pos.w*m_camera.toPer.m[3][1];
		tmp.m_pos.w = 
			m_renderList.list[i].va.m_pos.x*m_camera.toPer.m[0][3] +
			m_renderList.list[i].va.m_pos.y*m_camera.toPer.m[1][3] +
			m_renderList.list[i].va.m_pos.z*m_camera.toPer.m[2][3] +
			m_renderList.list[i].va.m_pos.w*m_camera.toPer.m[3][3];

		m_renderList.list[i].va.m_pos.x = tmp.m_pos.x/tmp.m_pos.w;
		m_renderList.list[i].va.m_pos.y = tmp.m_pos.y/tmp.m_pos.w;
		m_renderList.list[i].va.m_pos.z = 0;
		m_renderList.list[i].va.m_pos.w = 1.0;

//---------------------------------------------------------------------
		tmp.m_pos.x = 
			m_renderList.list[i].vb.m_pos.x*m_camera.toPer.m[0][0] +
			m_renderList.list[i].vb.m_pos.y*m_camera.toPer.m[1][0] +
			m_renderList.list[i].vb.m_pos.z*m_camera.toPer.m[2][0] +
			m_renderList.list[i].vb.m_pos.w*m_camera.toPer.m[3][0];
		tmp.m_pos.y = 
			m_renderList.list[i].vb.m_pos.x*m_camera.toPer.m[0][1] +
			m_renderList.list[i].vb.m_pos.y*m_camera.toPer.m[1][1] +
			m_renderList.list[i].vb.m_pos.z*m_camera.toPer.m[2][1] +
			m_renderList.list[i].vb.m_pos.w*m_camera.toPer.m[3][1];
		tmp.m_pos.w = 
			m_renderList.list[i].vb.m_pos.x*m_camera.toPer.m[0][3] +
			m_renderList.list[i].vb.m_pos.y*m_camera.toPer.m[1][3] +
			m_renderList.list[i].vb.m_pos.z*m_camera.toPer.m[2][3] +
			m_renderList.list[i].vb.m_pos.w*m_camera.toPer.m[3][3];

		m_renderList.list[i].vb.m_pos.x = tmp.m_pos.x/tmp.m_pos.w;
		m_renderList.list[i].vb.m_pos.y = tmp.m_pos.y/tmp.m_pos.w;
		m_renderList.list[i].vb.m_pos.z = 0;
		m_renderList.list[i].vb.m_pos.w = 1.0;

//-------------------------------------------------------------------------------
		tmp.m_pos.x = 
			m_renderList.list[i].vc.m_pos.x*m_camera.toPer.m[0][0] +
			m_renderList.list[i].vc.m_pos.y*m_camera.toPer.m[1][0] +
			m_renderList.list[i].vc.m_pos.z*m_camera.toPer.m[2][0] +
			m_renderList.list[i].vc.m_pos.w*m_camera.toPer.m[3][0];
		tmp.m_pos.y = 
			m_renderList.list[i].vc.m_pos.x*m_camera.toPer.m[0][1] +
			m_renderList.list[i].vc.m_pos.y*m_camera.toPer.m[1][1] +
			m_renderList.list[i].vc.m_pos.z*m_camera.toPer.m[2][1] +
			m_renderList.list[i].vc.m_pos.w*m_camera.toPer.m[3][1];
		tmp.m_pos.w = 
			m_renderList.list[i].vc.m_pos.x*m_camera.toPer.m[0][3] +
			m_renderList.list[i].vc.m_pos.y*m_camera.toPer.m[1][3] +
			m_renderList.list[i].vc.m_pos.z*m_camera.toPer.m[2][3] +
			m_renderList.list[i].vc.m_pos.w*m_camera.toPer.m[3][3];

		m_renderList.list[i].vc.m_pos.x = tmp.m_pos.x/tmp.m_pos.w;
		m_renderList.list[i].vc.m_pos.y = tmp.m_pos.y/tmp.m_pos.w;
		m_renderList.list[i].vc.m_pos.z = 0;
		m_renderList.list[i].vc.m_pos.w = 1.0;
	}
}

void CGraphics::gTranslateToViewport()
{
	for(int i=0; i<m_renderList.m_faceNum; ++i)
	{
		if(m_renderList.list[i].m_state != CTriangle::STATE_VISIABLE)
			continue;

		m_renderList.list[i].va.m_pos.x = (m_renderList.list[i].va.m_pos.x + 1) * (SCREEN_HALF_WIDTH - 0.5);
		m_renderList.list[i].va.m_pos.y = (SCREEN_HEIGHT - 1) - (m_renderList.list[i].va.m_pos.y + 1) * (SCREEN_HALF_HEIGHT - 0.5);

		m_renderList.list[i].vb.m_pos.x = (m_renderList.list[i].vb.m_pos.x + 1) * (SCREEN_HALF_WIDTH - 0.5);
		m_renderList.list[i].vb.m_pos.y = (SCREEN_HEIGHT - 1) - (m_renderList.list[i].vb.m_pos.y + 1) * (SCREEN_HALF_HEIGHT - 0.5);

		m_renderList.list[i].vc.m_pos.x = (m_renderList.list[i].vc.m_pos.x + 1) * (SCREEN_HALF_WIDTH - 0.5);
		m_renderList.list[i].vc.m_pos.y = (SCREEN_HEIGHT - 1) - (m_renderList.list[i].vc.m_pos.y + 1) * (SCREEN_HALF_HEIGHT - 0.5);
	}
}